<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分数" prop="score">
      <el-input v-model="dataForm.score" placeholder="分数"></el-input>
    </el-form-item>
    <el-form-item label="打分时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="打分时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="离开房间时间" prop="leaveTime">
      <el-input v-model="dataForm.leaveTime" placeholder="离开房间时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()" :loading="loading">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import * as weekscore from "@/api/weekscore";
    export default {
    data () {
      return {
        visible: false,
        loading: false,
        dataForm: {
          id: 0,
          score: '',
          createTime: '',
          updateTime: '',
          leaveTime: ''
        },
        dataRule: {
          score: [
            { required: true, message: '分数不能为空', trigger: 'blur' }
          ],          createTime: [
            { required: true, message: '打分时间不能为空', trigger: 'blur' }
          ],          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],          leaveTime: [
            { required: true, message: '离开房间时间不能为空', trigger: 'blur' }
          ]        }
      }
    },
    methods: {
      init (id) {
        if (id) {
            this.dataForm.id = id
        }
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
             weekscore.info({id}).then( data => {
                 this.dataForm = data;
              });
          }
        })
      },

      handlerUpdate() {
          this.loading = true
          weekscore.update(this.dataForm).then(data => {
              this.$message.success('编辑成功')
              this.loading = false
              this.visible = false
              this.$emit('refreshDataList')
          }).catch(()=>{
              this.visible = false
          })
      },

      handlerSave() {
          this.loading = true
              weekscore.save(this.dataForm).then(data => {
              this.$message.success('新增成功')
              this.loading = false
              this.visible = false
              this.$emit('refreshDataList')
          }).catch(()=>{
              this.loading = false
              this.visible = false
              this.$message.error("操作失败")
          })
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (!valid)return;
            if (!this.dataForm.id) {
                this.handlerSave()
            }else {
                this.handlerUpdate()
            }
        })
      }
    }
  }
</script>
